package com.cskaoyan.exeception;

import com.cskaoyan.bean.BaseRespVo;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

@RestControllerAdvice
public class GenericExeceptionControllerAdvice {

    /**
     * 数据库事务需要手动失败可抛这个异常
     * @param sqlException
     * @return
     */
    @ExceptionHandler(SQLException.class)
    public BaseRespVo sqlException(SQLException sqlException) {
        sqlException.printStackTrace();//打印 要不开发时看不到报错了
        String message = sqlException.getMessage();
        return BaseRespVo.fail(message);
    }

    /**
     * 捕获运行时异常 打印到后台，发消息给前端（改message为 系统繁忙）
     * @param runtimeException
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    public BaseRespVo runtimeException(RuntimeException runtimeException) {
        runtimeException.printStackTrace();//打印 要不开发时看不到报错了
        String message = runtimeException.getMessage();
        return BaseRespVo.fail(message);
    }

}
